package com.sunyard.manage.service.impl;

import com.sunyard.constant.ErrMessageConst;
import com.sunyard.dal.bo.PubConfigBO;
import com.sunyard.dal.dto.OtherParamDTO;
import com.sunyard.dal.dto.PubConfigDTO;
import com.sunyard.dal.entity.OtherParam;
import com.sunyard.dal.mapper.OtherParamMapper;
import com.sunyard.manage.service.PubConfigService;
import com.sunyard.utils.Response.BaseResult;
import com.sunyard.utils.Response.GetResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class PubConfigServiceImpl implements PubConfigService {


    @Autowired
    private OtherParamMapper otherParamMapper;
    @Override
    public BaseResult<List<OtherParamDTO>> queryReview() {
        List<String> list = new ArrayList<>();
        list.add(OtherParam.OPERATOR_ADD_AUTH);
        list.add(OtherParam.LOCK_ADD_AUTH);
        list.add(OtherParam.KEY_ADD_AUTH);
        list.add(OtherParam.FINGER_ADD_AUTH);
        list.add(OtherParam.TASK_ADD_AUTH);
        List<OtherParamDTO> otherParams = otherParamMapper.queryByKeys(list);
        return GetResult.getResult(otherParams);
    }

    @Override
    public BaseResult updateReview(PubConfigBO pubConfigBO) {
        try {
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            String format = dateFormat.format(date);
            for (PubConfigDTO pubConfigDTO : pubConfigBO.getPubConfigDTOS()) {
                pubConfigDTO.setUpdateDatetime(format);
                otherParamMapper.updateByKey(pubConfigDTO);
            }
        } catch (Exception e) {
            log.error("动态复核配置修改失败", e);
            return GetResult.getResultErr("修改失败");
        }
        return GetResult.getResultSuccess("修改成功");
    }

    @Override
    public BaseResult<List<OtherParamDTO>> queryWarn() {
        List<String> list = new ArrayList<>();
        list.add(OtherParam.UNLOCK_WARN);
        list.add(OtherParam.UPLOAD_LOG_KEY_WARN);
        list.add(OtherParam.UPLOAD_LOG_ORG_WARN);
        list.add(OtherParam.USER_INFO_WARN);
        list.add(OtherParam.WORK_TIME_WARN);
        List<OtherParamDTO> otherParams = otherParamMapper.queryByKeys(list);
        return GetResult.getResult(otherParams);
    }

    @Override
    public BaseResult updateWarn(PubConfigBO pubConfigBO) {
        try {
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            String format = dateFormat.format(date);
            for (PubConfigDTO pubConfigDTO : pubConfigBO.getPubConfigDTOS()) {
                pubConfigDTO.setUpdateDatetime(format);
                otherParamMapper.updateByKey(pubConfigDTO);
            }
        } catch (Exception e) {
            log.error("动态复核配置修改失败", e);
            return GetResult.getResultErr("修改失败");
        }
        return GetResult.getResultSuccess("修改成功");
    }
}
